/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.domain.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.IteratorUtils;
import org.cshoo.tattoo.datacenter.domain.bo.MessageBo;
import org.cshoo.tattoo.datacenter.domain.po.Message;
import org.cshoo.tattoo.datacenter.domain.po.MessageItem;
import org.cshoo.tattoo.datacenter.domain.repository.IMessageItemRepository;
import org.cshoo.tattoo.datacenter.domain.repository.IMessageRepository;
import org.cshoo.tattoo.embed.infra.entity.PageQueryEntity;
import org.cshoo.tattoo.embed.infra.exception.ValidateException;
import org.cshoo.tattoo.embed.infra.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
public class MessageService {

    @Resource
    private IMessageRepository messageRepository;

    @Resource
    private IMessageItemRepository messageItemRepository;

    /**
     * 判断此编号是否存在
     *
     * @param code Message编号
     * @return 判断结果
     */
    public boolean checkMessageCodeExists(String code) {
        Message message = messageRepository.findByCode(code);
        return Objects.nonNull(message);
    }

    /**
     * 添加一个Message
     *
     * @param message 添加Message
     */
    public void addMessage(Message message) {
        messageRepository.save(message);
    }

    /**
     * 修改一个Message
     *
     * @param message 修改Message请求VO
     */
    public void updateMessage(Message message) {
        message.setLastModifiedBy(UserUtils.getUser().getUsername());
        message.setLastModifiedDate(new Date());
        messageRepository.updateMessage(message);
    }

    /**
     * 删除一个Message
     *
     * @param id id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMessage(Long id) {
        log.info("id:{}", id);
        messageItemRepository.deleteByMessageId(id);
        messageRepository.deleteById(id);
    }

    /**
     * 查询Message列表
     *
     * @return Message列表
     */
    @SuppressWarnings("unchecked")
    public List<Message> queryMessages() {
        Iterable<Message> MessageIterable = messageRepository.findAll();
        return IteratorUtils.toList(MessageIterable.iterator());
    }

    /**
     * 分页查询
     *
     * @param messagePageReq 分页查询对象
     * @return 分页查询结果
     */
    public Page<Message> queryMessages(PageQueryEntity<String> messagePageReq) {
        PageRequest pageRequest = PageRequest.of(messagePageReq.getPageNumber(), messagePageReq.getPageSize());
        String keyword = messagePageReq.getQuery();
        return messageRepository.queryMessage(keyword, pageRequest);
    }

    /**
     * 根据messageId查询item
     *
     * @param messageId messageId
     * @return item列表
     */
    public List<MessageItem> queryMessageItems(Long messageId) {
        log.info("messageId:{}", messageId);
        return messageItemRepository.findByMessageId(messageId);
    }

    /**
     * 查询message item
     *
     * @param messagePageReq 分页请求对象
     * @return message item
     */
    public Page<MessageItem> queryMessageItems(PageQueryEntity<Long> messagePageReq) {
        Long MessageId = messagePageReq.getQuery();
        PageRequest pageRequest = PageRequest.of(messagePageReq.getPageNumber(), messagePageReq.getPageSize());
        return messageItemRepository.findByMessageIdOrderByLang(MessageId, pageRequest);
    }

    /**
     * 查询单个的Message
     *
     * @param code 编号
     * @return Message信息
     */
    public MessageBo querySingleMessage(String code) {
        Message message = messageRepository.findByCode(code);
        MessageBo messageBo = new MessageBo();
        BeanUtils.copyProperties(message, messageBo);
        List<MessageItem> messageItems = messageItemRepository.findByMessageId(message.getId());
        messageBo.setMessageItems(messageItems);
        return messageBo;
    }

    /**
     * 根据code和MessageId查看是否存在MessageItem
     *
     * @param lang      lang
     * @param messageId messageId
     * @return 查看存在结果
     */
    public boolean checkMessageItemCodeExists(String lang, Long messageId) {
        log.info("lang:{},messageId:{}", lang, messageId);
        return !Objects.isNull(messageItemRepository.findByLangAndMessageId(lang, messageId));
    }

    /**
     * 添加一个Message项
     *
     * @param messageItem 添加Message项VO
     */
    public void addMessageItem(MessageItem messageItem) throws Exception {
        Long messageId = messageItem.getMessageId();
        String lang = messageItem.getLang();
        MessageItem checkMessageItem = messageItemRepository.findByLangAndMessageId(lang, messageId);
        if (Objects.nonNull(checkMessageItem)) {
            throw new ValidateException("duplicate");
        }

        messageItemRepository.save(messageItem);
    }

    /**
     * 修改一个Message项
     *
     * @param messageItem 修改Message
     */
    public void updateMessageItem(MessageItem messageItem) {
        log.info("messageItem:{}", messageItem);
        Optional<MessageItem> messageItemOptional = messageItemRepository.findById(messageItem.getId());
        if (messageItemOptional.isPresent()) {
            messageItemRepository.save(messageItem);
        }
    }

    /**
     * 删除一个Message项
     *
     * @param id id
     */
    public void deleteMessageItem(Long id) {
        log.info("id:{}", id);
        messageItemRepository.deleteById(id);
    }
}